package com.ruoyi.web.controller.sujian;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SjWaterCost;
import com.ruoyi.system.domain.SjWaterCost;
import com.ruoyi.system.domain.SjWaterCost;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.service.ISysFloorService;
import com.ruoyi.system.service.IWaterCostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 *
 * @date 2021-07-05
 */
@Controller
@RequestMapping("/sujian/waterCost")
public class WaterCostController extends BaseController
{
    private String prefix = "propertys/sujian/waterRateManagement/cost";

    @Autowired
    private ISysFloorService floorInfoService;

    @Autowired
    private IWaterCostService waterCostService;

    @GetMapping()
    public String waterCost(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/main";
    }

    /**
     * 查询员工考勤列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjWaterCost sjWaterCost)
    {
        startPage();
        List<SjWaterCost> sjWaterCostList = waterCostService.selectWaterCostList(sjWaterCost);
        return getDataTable(sjWaterCostList);
    }

    @PostMapping("/generateWater")
    @ResponseBody
    public AjaxResult generateWater(String etldt) {
        //查询该批次是否存在
        SjWaterCost sjWaterCost = new SjWaterCost();
        etldt = etldt.replaceAll("-","");
        sjWaterCost.setEtldt(etldt);
        List<SjWaterCost> sjWaterCostList = waterCostService.selectWaterCostList(sjWaterCost);
        if(sjWaterCostList!=null){
            //删除之前数据  重新生成?
            waterCostService.deleteWaterByEtldt(etldt);
        }
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        if(floorInfos!=null&&floorInfos.size()>0){
            waterCostService.generateWater(etldt,floorInfos);
        }else{
            return AjaxResult.error("未维护楼层");
        }
        return AjaxResult.success();
    }

    //导入水费数据
    @PostMapping("/importExcel")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SjWaterCost> util = new ExcelUtil<>(SjWaterCost.class);
        List<SjWaterCost> lists = util.importExcel(file.getInputStream());
        //校验数据
        String message = waterCostService.importWaterCost(lists);
        return AjaxResult.success(message);
    }

    //导入水费数据模板
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<SjWaterCost> util = new ExcelUtil<SjWaterCost>(SjWaterCost.class);
        return util.importTemplateExcel("水费信息数据");
    }

    //导出
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjWaterCost sjWaterCost) {
        List<SjWaterCost> list = waterCostService.exportWaterCost(sjWaterCost);
        ExcelUtil<SjWaterCost> util = new ExcelUtil(SjWaterCost.class);
        return util.exportExcel(list, "水费数据");
    }
}
